/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.sport.kreth.trainingsstunden.iPdf;

import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


/**
 *
 * @author markus
 */
public class IPdf {
   static org.w3c.dom.Document doc;
   
   private void initDoc(String datei){
       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            final DocumentBuilder builder = factory.newDocumentBuilder();
            try {
                doc = builder.parse(datei);
            } catch (SAXException ex) {
                Logger.getLogger(IPdf.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(IPdf.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(IPdf.class.getName()).log(Level.SEVERE, null, ex);
        }
   }
   
   private String getCaption(){
       NodeList list= doc.getElementsByTagName("h2");
       if(list.getLength() >0)
           return list.item(0).getTextContent();
       return null;
   }
   
    private String[] getText(){
        final NodeList paragraphs = doc.getElementsByTagName("p");
        String[] texts = new String[paragraphs.getLength()];
        for (int i = 0; i < paragraphs.getLength(); i++) {
            texts[i] = paragraphs.item(i).getTextContent();
        }
        return texts;
    }
    
    public void create(String quelle, String ziel) {
        create(quelle, ziel);
    }
    
    public void create(String quelle, String ziel, String bild) {
        try {
            try {
                Document document = new Document(PageSize.A4);
                PdfWriter.getInstance(document, new FileOutputStream(ziel));
                Font font_ueberschrift = new Font(Font.FontFamily.HELVETICA, 16, Font.BOLD);
                Font f2 = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.ITALIC);
                Font font_text = new Font(Font.FontFamily.HELVETICA, 12, Font.NORMAL);
                document.open();
                initDoc(quelle);
                document.addAuthor("Markus Kreth");
//                document.addTitle("Das ist ein Überfall");
                Paragraph title = new Paragraph(getCaption(), font_ueberschrift);
                title.setAlignment(Element.ALIGN_CENTER);
                document.add(new Paragraph());
//                title.add("\n\nAutor: Markus Kreth");
                document.add(title);
                if (bild != null) document.add(Image.getInstance(bild));
//                Chapter ch = new Chapter(new Paragraph("Das ist ein Überfall", font_ueberschrift),1);
//                ch.addSection(new Paragraph("Ein Überfall mit etwas anderen Folgen...", f2));
                
//                document.add(new Paragraph(getCaption(), font_ueberschrift));
                String[] texte = getText();
                for (int i = 0; i < texte.length; i++) {
                    document.add(new Paragraph(texte[i], font_text));
                    
                }
//                document.add(ch);
                document.close();
            } catch (IOException ex) {
                Logger.getLogger(IPdf.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (DocumentException ex) {
            Logger.getLogger(IPdf.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
   
   public static void main(String[] str){
//       new IPdf().create("text.xml", "output3.pdf");
       new IPdf().create("story2/text.xml", "output4.pdf", "story2/bild1.jpg");
   }
    
}
